import _ from 'lodash'
export default {
  name: 'page-mixin',
  data () {
    return {
      mixinViewModuleOptions: {
        getDataListURL: '', // 数据列表接口，API地址
        getDataListIsPage: true, // 数据列表接口，是否需要分页？
        deleteURL: '', // 删除接口，API地址
        deleteIsBatchURL: '', // 删除接口，是否需要批量？
        deleteIsBatchKey: 'id', // 删除接口，批量状态下由那个key进行标记操作？比如：pid，uid...
        isSetData: false, // 获取的数据是否进去处理 func => setData
        isChangeData: false // 请求数据是否进去处理 func => changeData
      },
      isEdit: false,
      queryParams: {}, // 查询条件
      searchParams: {}, // 特殊加长的搜索条件
      pagination: {
        pageNum: 1, // 当前页码
        pageSize: 15, // 每页条数
        rowsNumber: 0 // 总条数
      },
      selected: [],
      isTableLoading: false,
      tableData: [],
      cloneTableData: [],
      visibleColumns: [],
      newColumns: {},
      notUncheckableColumns: ['operations'] // 不可取消显示的表格列
    }
  },
  computed: {
    tableColumns () {
      this.visibleColumns = Object.keys(this.newColumns).filter(v => !_.isNil(v))
      const newColumn = Object.keys(this.newColumns).reduce((arr, v) => {
        if (!_.isNil(this.newColumns[v])) {
          let item = { name: v, field: v, label: this.newColumns[v], align: v === 'operations' ? 'center' : 'left' }
          arr.push(item)
        }
        return arr
      }, [])
      return newColumn
    },
    isDisDelBatch () {
      return !this.selected.length
    }
  },
  methods: {
    onSubmit (params) {
      this.selected = []
      this.pagination.pageNum === 1 ? this.getList() : this.pagination.pageNum = 1
    },
    // 重置数据
    resetSearch () {
      this.selected = []
      this.queryParams = Object.assign({}, this.$options.data.call(this).queryParams)
    },
    // 获取列表
    getList () {
      this.isTableLoading = true
      const member = {
        ...this.queryParams,
        ...this.pagination
      }
      !_.isEmpty(this.searchParams) && Object.assign(member, this.searchParams)
      const params = this.mixinViewModuleOptions.isChangeData ? this.changeData(member) : member
      this.mixinViewModuleOptions.getDataListURL(params).then((result) => {
        this.isTableLoading = false
        const ret = result.list || result.data || result.records || []
        this.tableData = this.$utils.setLabel(_.cloneDeep(ret), {})
        this.cloneTableData = _.cloneDeep(ret)
        this.mixinViewModuleOptions.isSetData && this.setData(this.tableData)
        this.pagination.rowsNumber = result.total || 0
      }).catch((err) => {
        this.isTableLoading = false
        console.log(err)
      })
    },
    // 单个删除
    onDelete (id) {
      this.isTableLoading = true
      this.mixinViewModuleOptions.deleteURL(id).then(res => {
        this.isTableLoading = false
        res.success && this.$message.success('删除成功')
        this.selected = []
        this.getList()
      }).catch(err => {
        this.isTableLoading = false
        console.log(err)
      })
    },
    // 批量删除
    onBatchDelete () {
      const ids = this.selected.map(v => v[this.mixinViewModuleOptions.deleteIsBatchKey])
      this.$utils.showMessage('icontubiao-28', '是否确认全部删除?', () => {
        this.mixinViewModuleOptions.deleteIsBatchURL(ids).then((res) => {
          this.$message.success('删除成功')
          this.selected = []
          this.getList()
        }).catch((err) => {
          console.log(err)
        })
      }, true)
    },
    // 模糊查询
    inputSearch (item, val) {
      this.searchParams.searchBy = item.id
      this.searchParams.keyword = val
      this.selected = []
      this.onSubmit()
    },
    // 清空搜索值
    clearSearchVal () {
      this.searchParams.searchBy = null
      this.searchParams.keyword = null
      this.searchParams = {}
      this.onSubmit()
    },
    // 刷新
    // 弹窗关闭时刷新数据
    onRefresh (isRefresh) {
      isRefresh && this.onSubmit()
    },
    // 按照时间排序
    onSeletByTime () {
      this.queryParams.createTimeAsc = !this.queryParams.createTimeAsc
      this.getList()
    },
    // if input empty clear serach
    changeVal (val) {
      if (_.isEmpty(val)) {
        this.searchParams.searchBy = null
        this.searchParams.keyword = null
        this.searchParams = {}
      }
    }
  }
}
